# Contributor: Alex Dowad <alexinbeijing@gmail.com>
# Maintainer: Alex Dowad <alexinbeijing@gmail.com>
pkgname=lua-luautf8
pkgver=0.1.3
pkgrel=2
pkgdesc="A UTF-8 support module for Lua"
url="https://github.com/starwing/luautf8"
arch="all"
license="MIT"
source="$pkgname-$pkgver.tar.gz::https://github.com/starwing/luautf8/archive/$pkgver.tar.gz"
builddir="$srcdir/${pkgname#lua-}-$pkgver"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

prepare() {
	default_prepare

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		${CC:-gcc} $CFLAGS -fPIC $(pkg-config --cflags lua$lver) lutf8lib.c $LDFLAGS -shared -o lua-utf8.so
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing on Lua $lver"

		cd "$builddir-$lver"
		lua$lver test.lua
		lua$lver test_compat.lua
		lua$lver test_pm.lua
	done
}

package() {
	mkdir -p "$pkgdir"
	local lver; for lver in $_luaversions; do
		install -m 755 -D "$builddir-$lver"/lua-utf8.so \
			"$pkgdir"/usr/lib/lua/$lver/lua-utf8.so
	done
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	amove usr/lib/lua/$lver
}

sha512sums="
d0abe73fecbfeba36083c135d77962d953ad9e431721a2557669c912a2703e31c46f84f2ee9ee4ac966a5bcc88d4daef930f8d2d8e72106fa61010509c183e29  lua-luautf8-0.1.3.tar.gz
"
